function HandleDialogDetails(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  
  this._purchases = (options && options.purchases) ? options.purchases : [];
  this._orderId = (options && options.orderId) ? options.orderId : null;
  this._hidePrice = (options && options.hidePrice) ? options.hidePrice : false;
  this._pur = (options && options.pur) ? options.pur : false;
  
  this._createElements();
};

HandleDialogDetails.prototype._createElements = function() {
  var _self = this;
  DlmanageUtils.clear(this._gui);
  if (this._pur) {//不是流程、这个只是purchaseitem。
    this._albums = [];
    if (this._item.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || this._item.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}') {
      this._albums[this._albums.length] = this._item;//如果是相册或者雑志，则加进去
    }
    
    //如果这个purchase已经删除掉了，那么在显示设计的时候都只显示自己。所以this._albums数组中就不需要再添加purchase了
    //否则找到this._purchases所有别的相册或者雑志。
    if (this._item.cancelled == 0) {
      for (var i = 0, il = this._purchases.length; i < il; i++) {
        var purchase = this._purchases[i];
        if (purchase.cancelled == 1) {
          continue;
        }
        if (purchase.trackId != this._item.trackId && (purchase.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || purchase.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) {
          this._albums[this._albums.length] = purchase;
        }
      }
    }
    
    MiscUtils.sort(this._albums, ['otherId']);
    //根据相册的数组，并根据otherId来进行分类---相同otherId的放一起。
    this._designs = {};
    for (var i = 0, il = this._albums.length; i < il; i++) {
      var album = this._albums[i];
     
      if (this._designs[album.otherId] == undefined) {
        this._designs[album.otherId] = [album];
      } else {
        this._designs[album.otherId][this._designs[album.otherId].length] = album;
      }
    }
  
    this._gui.sample.style.color = '#ff0000';
    this._gui.sample.appendChild(document.createTextNode(this._item.name));
    this._gui.quantity.style.color = '#ff0000';
    this._gui.quantity.appendChild(document.createTextNode(this._item.quantity));
    this._gui.price.style.color = '#ff0000';
    
    this._gui.quantityPic.style.display = 'none';
    this._gui.quantity.style.display = 'none';
    
    this._gui.pricePic.style.display = (this._hidePrice) ? 'none' : 'block';
    
    this._gui.price.appendChild(document.createTextNode((this._hidePrice) ? '' : this._item.total));
    
    for (var i = 0, il = this._item.selects.length; i < il; i++) {
      var select = this._item.selects[i];
      var gselect = this._gui.selects[i];
      gselect.name.appendChild(document.createTextNode(select.title));
      gselect.option.style.color = '#ff0000';
      gselect.option.appendChild(document.createTextNode(select.value));
    }
    
  } else {
    
    this._albums = [];
    if (this._item.pi_type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || this._item.pi_type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}') {
      this._albums[this._albums.length] = this._item;
    }
    if (this._item.cancelled == 0) {
      for (var i = 0, il = this._purchases.length; i < il; i++) {
        var purchase = this._purchases[i];
        if (purchase.cancelled == 1) {
          continue;
        }
        if (purchase.trackId != this._item.pi_trackId && (purchase.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || purchase.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) {
          this._albums[this._albums.length] = purchase;
        }
      }
    }
    MiscUtils.sort(this._albums, ['pi_otherId']);

    this._designs = {};
    for (var i = 0, il = this._albums.length; i < il; i++) {
      var album = this._albums[i];
      
      if (this._designs[album.pi_otherId] == undefined) {
        this._designs[album.pi_otherId] = [album];
      } else {
        this._designs[album.pi_otherId][this._designs[album.pi_otherId].length] = album;
      }
    }
    this._gui.sample.style.color = '#ff0000';
    this._gui.sample.appendChild(document.createTextNode(this._item.pi_name));
    this._gui.quantity.style.color = '#ff0000';
    this._gui.quantity.appendChild(document.createTextNode(this._item.pi_quantity));
    this._gui.price.style.color = '#ff0000';
    
    this._gui.quantityPic.style.display = 'none';
    this._gui.quantity.style.display = 'none';
    
    this._gui.pricePic.style.display = (this._hidePrice) ? 'none' : 'block';
    
    this._gui.price.appendChild(document.createTextNode((this._hidePrice) ? '' : this._item.pi_total));
    
    for (var i = 0, il = this._item.pi_selects.length; i < il; i++) {
      var select = this._item.pi_selects[i];
      var gselect = this._gui.selects[i];
      gselect.name.appendChild(document.createTextNode(select.title));
      gselect.option.style.color = '#ff0000';
      gselect.option.appendChild(document.createTextNode(select.value));
    }
  }
  
  this._loadData();
};

HandleDialogDetails.prototype._loadData = function() {
  this._produces = null;

  this._retrieveProduces();
};

HandleDialogDetails.prototype._verifyData = function() {
  if (this._produces) {
    this._updateElements();
  }
};

HandleDialogDetails.prototype._retrieveProduces = function() {
  var _self = this;
  if (this._gui.design && this._orderId) {
    this._gui.design.appendChild(DOMUtils.getLoadingImage());
    var joins = '&selects=0' + '&purchaseitems=1' + '&preconusers=0' + '&events=0' + '&tracks=0' + '&produces=0' + '&notes=0';
    var args = '&c=d.branch = \'' + '{{$smarty.const.BRANCH|escape:javascript}}' + '\'' + ' AND ' + 'd.orderId=' + this._orderId + joins;
    new RequestUtils()._mysql('produce', args, function(result, params) { _self._produces = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, { php: Constant.FACTORY + '/customphp/produceread.php' });
  } else {
    this._produces = [];
    this._updateElements();
  }
};

HandleDialogDetails.prototype._updateElements = function() {
  var table, td, tr;
  if (this._pur) {//未生成流程的purchaseitem
    var exists = false;
    if (this._gui.design) {
      DOMUtils.removeChildElements(this._gui.design);
      table = document.createElement('table');
      this._gui.design.appendChild(table);
      
      for (var i = 0, il = this._produces.length; i < il; i++) {
        var produce = this._produces[i];
        if (produce.pi_model == '{{$smarty.const.MODEL_NAME_ALBUM_DESIGN|escape:javascript}}' && produce.pi_trackId == this._item.otherId) {
          //如果purchase未删除，且有相册设计流程。则显示为相册设计的title
          //否则显示该产品的name
          exists = true;
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode('相册设计 (' + ((this._item.cancelled == 1) ? this._item.name : produce.title) + ')'));
        }
      }
      //没有相册流程，则只显示单独或者共用的设计
      if (!exists && this._designs[this._item.otherId]) {
        for (var otherId in this._designs) {
          var other = DlmanageUtils.congregate2(this._designs[otherId]);
          if (this._item.otherId != otherId) {
            continue;
          }
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode('相册设计 (' + other.title + ')'));
        }
      }
    }
    
  } else {
    var exists = false;
    if (this._gui.design) {
      DOMUtils.removeChildElements(this._gui.design);
      table = document.createElement('table');
      this._gui.design.appendChild(table);
      
      for (var i = 0, il = this._produces.length; i < il; i++) {
        var produce = this._produces[i];
        if (this._item.pi_model == '{{$smarty.const.MODEL_NAME_ALBUM_DESIGN|escape:javascript}}' && produce.pi_trackId == this._item.pi_trackId) {
          // 如果this._item是相册设计流程，则显示其包含的相册。
          exists = true;
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode('相册 (' + DlmanageUtils.congregate4(produce.v2_purchaseItems).title + ')'));
        } else if (this._item.cancelled == 0 && this._item.pi_model == '{{$smarty.const.MODEL_NAME_PRODUCT_COMMON|escape:javascript}}' && (this._item.pi_type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || this._item.pi_type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}') && produce.pi_trackId == this._item.pi_otherId) {
          //如果this._item是相册流程,那么根据该相册的otherId是相册设计的pi_trackId来判断，条件成立显示相册设计。
          exists = true;
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode('相册设计 (' + produce.title + ')'));
        }
      }
      
      if (!exists && this._designs[this._item.pi_otherId]) {
        for (var otherId in this._designs) {
          var other = DlmanageUtils.congregate3(this._designs[otherId]);
          if (this._item.pi_otherId != otherId) {
            continue;
          }
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode('相册设计 (' + other.title + ')'));
        }
      }
    }
  }
};
